import { PrismaClient } from "@prisma/client";

function buildClient() {
  return new PrismaClient();
}

export type PrismaClientType = ReturnType<typeof buildClient>;

let prisma: PrismaClientType;

declare global {
  // eslint-disable-next-line no-var
  var __prisma: PrismaClientType | undefined;
}

if (process.env.NODE_ENV === "production") {
  prisma = buildClient();
  prisma.$connect();
} else {
  if (!global.__prisma) {
    global.__prisma = buildClient();
    global.__prisma.$connect();
  }
  prisma = global.__prisma;
}

export default prisma;